﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using SF.DBUtility;//Please add references
using SF.Common;

namespace SF.DAL
{
    /// <summary>
    /// 数据访问类:sf_contract_child
    /// </summary>
    public partial class sf_contract_child
    {
        private string databaseprefix;
        public sf_contract_child(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "contract_child");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt,8)			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SF.Model.sf_contract_child model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sf_contract_child(");
            strSql.Append("contract_num,appNum,pay_type,price_one,price_low,price_low_one,price_height,price_height_two,create_date,create_people,update_date,IsDelete)");
            strSql.Append(" values (");
            strSql.Append("@contract_num,@appNum,@pay_type,@price_one,@price_low,@price_low_one,@price_height,@price_height_two,@create_date,@create_people,@update_date,@IsDelete)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@contract_num", SqlDbType.NVarChar,100),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@pay_type", SqlDbType.Int,4),
					new SqlParameter("@price_one", SqlDbType.Float,8),
					new SqlParameter("@price_low", SqlDbType.Float,8),
					new SqlParameter("@price_low_one", SqlDbType.Float,8),
					new SqlParameter("@price_height", SqlDbType.Float,8),
					new SqlParameter("@price_height_two", SqlDbType.Float,8),
					new SqlParameter("@create_date", SqlDbType.DateTime),
					new SqlParameter("@create_people", SqlDbType.NVarChar,100),
					new SqlParameter("@update_date", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Int,4)};
            parameters[0].Value = model.contract_num;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.pay_type;
            parameters[3].Value = model.price_one;
            parameters[4].Value = model.price_low;
            parameters[5].Value = model.price_low_one;
            parameters[6].Value = model.price_height;
            parameters[7].Value = model.price_height_two;
            parameters[8].Value = model.create_date;
            parameters[9].Value = model.create_people;
            parameters[10].Value = model.update_date;
            parameters[11].Value = model.IsDelete;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SF.Model.sf_contract_child model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + databaseprefix + "contract_child set ");
            strSql.Append("contract_num=@contract_num,");
            strSql.Append("appNum=@appNum,");
            strSql.Append("pay_type=@pay_type,");
            strSql.Append("price_one=@price_one,");
            strSql.Append("price_low=@price_low,");
            strSql.Append("price_low_one=@price_low_one,");
            strSql.Append("price_height=@price_height,");
            strSql.Append("price_height_two=@price_height_two,");
            strSql.Append("create_date=@create_date,");
            strSql.Append("create_people=@create_people,");
            strSql.Append("update_date=@update_date,");
            strSql.Append("IsDelete=@IsDelete");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@contract_num", SqlDbType.NVarChar,100),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@pay_type", SqlDbType.Int,4),
					new SqlParameter("@price_one", SqlDbType.Float,8),
					new SqlParameter("@price_low", SqlDbType.Float,8),
					new SqlParameter("@price_low_one", SqlDbType.Float,8),
					new SqlParameter("@price_height", SqlDbType.Float,8),
					new SqlParameter("@price_height_two", SqlDbType.Float,8),
					new SqlParameter("@create_date", SqlDbType.DateTime),
					new SqlParameter("@create_people", SqlDbType.NVarChar,100),
					new SqlParameter("@update_date", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
            parameters[0].Value = model.contract_num;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.pay_type;
            parameters[3].Value = model.price_one;
            parameters[4].Value = model.price_low;
            parameters[5].Value = model.price_low_one;
            parameters[6].Value = model.price_height;
            parameters[7].Value = model.price_height_two;
            parameters[8].Value = model.create_date;
            parameters[9].Value = model.create_people;
            parameters[10].Value = model.update_date;
            parameters[11].Value = model.IsDelete;
            parameters[12].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool UpdateDel(int id)
        {
            string sql = "update " + databaseprefix + "contract_child set IsDelete =1 where id=" + id;
            int rows = DbHelperSQL.ExecuteSql(sql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sf_contract_child ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_contract_child GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,contract_num,appNum,pay_type,price_one,price_low,price_low_one,price_height,price_height_two,create_date,create_people,update_date,IsDelete from " + databaseprefix + "contract_child ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt,8)			};
            parameters[0].Value = id;

            SF.Model.sf_contract_child model = new SF.Model.sf_contract_child();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_contract_child DataRowToModel(DataRow row)
        {
            SF.Model.sf_contract_child model = new SF.Model.sf_contract_child();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = long.Parse(row["id"].ToString());
                }
                if (row["contract_num"] != null)
                {
                    model.contract_num = row["contract_num"].ToString();
                }
                if (row["appNum"] != null)
                {
                    model.appNum = row["appNum"].ToString();
                }
                if (row["pay_type"] != null && row["pay_type"].ToString() != "")
                {
                    model.pay_type = int.Parse(row["pay_type"].ToString());
                }
                if (row["price_one"] != null && row["price_one"].ToString() != "")
                {
                    model.price_one = decimal.Parse(row["price_one"].ToString());
                }
                if (row["price_low"] != null && row["price_low"].ToString() != "")
                {
                    model.price_low = decimal.Parse(row["price_low"].ToString());
                }
                if (row["price_low_one"] != null && row["price_low_one"].ToString() != "")
                {
                    model.price_low_one = decimal.Parse(row["price_low_one"].ToString());
                }
                if (row["price_height"] != null && row["price_height"].ToString() != "")
                {
                    model.price_height = decimal.Parse(row["price_height"].ToString());
                }
                if (row["price_height_two"] != null && row["price_height_two"].ToString() != "")
                {
                    model.price_height_two = decimal.Parse(row["price_height_two"].ToString());
                }
                if (row["create_date"] != null && row["create_date"].ToString() != "")
                {
                    model.create_date = DateTime.Parse(row["create_date"].ToString());
                }
                if (row["create_people"] != null)
                {
                    model.create_people = row["create_people"].ToString();
                }
                if (row["update_date"] != null && row["update_date"].ToString() != "")
                {
                    model.update_date = DateTime.Parse(row["update_date"].ToString());
                }
                if (row["IsDelete"] != null && row["IsDelete"].ToString() != "")
                {
                    model.IsDelete = int.Parse(row["IsDelete"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,contract_num,appNum,pay_type,price_one,price_low,price_low_one,price_height,price_height_two,create_date,create_people,update_date,IsDelete ");
            strSql.Append(" FROM sf_contract_child ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where IsDelete = 0 and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,contract_num,appNum,pay_type,price_one,price_low,price_low_one,price_height,price_height_two,create_date,create_people,update_date,IsDelete ");
            strSql.Append(" FROM " + databaseprefix + "contract_child ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM " + databaseprefix + "contract_child ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where contract_num = '" + strWhere + "'");
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from sf_contract_child T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + databaseprefix + "contract_child");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where IsDelete = 0 and" + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 获取AppId
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public object GetAppid(string appid) 
        {
            string sql = @"select appid from " + databaseprefix + "appidmanage where businessNum='{0}'";
            if (appid != null) 
            {
                sql = string.Format(sql, appid); 
            }
            return DbHelperSQL.GetSingle(sql.ToString());
        }

        #endregion  ExtensionMethod
    }
}

